<?php
    require_once("recaptchalib.php");
    class captcha {
        public function makeCaptcha(){
            $publickey = "6LcnGcASAAAAAAC2-Klt3i2q7qSIwDj4mRqgt9sC";
            echo recaptcha_get_html($publickey);
            // the page side for captcha.
            // for your own reCAPTCHA key, please visit www.google.com/recaptcha/
            $_SESSION["language"] == 1? $lang = "EN " : $lang = "NL ";
            require_once('recaptchalib.php');
            $privatekey = "6LcnGcASAAAAAEI35ZhejY2YcNkRw93QV4gim6D9";
            // THIS CODE WILL NOT WORK! a private key is required to make the code function, but for safety reasons, it has been left out in the source-code
            
            if (isset($_POST['submitted']) && $_POST['submitted'] == "true"){
                $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

                if (!$resp->is_valid) {
                    // What happens when the CAPTCHA was entered incorrectly
                    $_SESSION["message"] = ("wrong CAPTCHA.<br />");
                    return false;
                }
                if ($_POST['naam'] != " " && $_POST['mail'] != " " && $_POST['content']){
                    // what to do when all information is filled in	
                    $to = "phoenix_2000@live.nl";
                    $content = $_POST["content"] . "  name = " . $_POST["naam"] . " & mail = " . $_POST["mail"];
                    $subj = $lang . $_POST["subject"];
                    $headers = "From: automail@leandermolegraaf.nl";
                    $mail = mail($to, $subj, $content, $headers);
                    // sends an email to me, having the subject that the user filled in, with either EN or NL in front of it, while the mail initially comes from automail

                    if ($mail==true){
                        // what to when the mail is succesfully send and...
                        if($_SESSION['language'] == "en"){
                            //... is in english
                            echo ("<font color='green'> Your email has been succesfully send.<br /> I'll try to answer in less then 24 hours.</font>");
                        }
                        else{
                            //... is in dutch
                            echo ("<font color='green'. Uw bericht is met succes verzonden. <br /> ik probeer deze binnen 24 uur te beantwoorden.</font>");
                        }
                    }
                    else{
                        // what to do if an error occured
                        if($_SESSION['language'] == "en"){
                            echo ("<font color='red'> Your email was accepted, but could not be send.<br /> Please try again later, or contact me manually at<br />");
                        }
                        else{
                            echo ("<font color='red'> Uw email werdt geaccepteerd, maar kon niet worden verstuurd.<br /> Probeer het later opnieuw, of neem manueel contact op via<br />");
                        }
                        echo ("<img src='email.png' border='1' alt='email adres' /></font>");
                        // the final code, adding a picture, is identical, so it is added after the if instead of seperately
                    }
                }
            }
        }

        public function checkCaptcha(){
            $privatekey = "6LcnGcASAAAAAEI35ZhejY2YcNkRw93QV4gim6D9";
            $resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

            if(!$resp->is_valid){
                return false;
            }
            else{
                return true;
            }
        }
    }
?>
